package jmetal.metaheuristics.singleObjective.island;

import java.util.HashMap;

import jmetal.core.Algorithm;
import jmetal.core.Operator;
import jmetal.core.Problem;
import jmetal.core.SolutionSet;
import jmetal.metaheuristics.singleObjective.geneticAlgorithm.gGA;
import jmetal.operators.crossover.CrossoverFactory;
import jmetal.operators.mutation.MutationFactory;
import jmetal.operators.selection.SelectionFactory;
import jmetal.problems.singleObjective.Sphere;
import jmetal.util.JMException;

public class IslandLTMain {
	public static void main(String [] args) throws JMException, ClassNotFoundException {
	    Problem   problem   ;         // The problem to solve
	    Algorithm algorithm ;         // The algorithm to use
	    Operator  crossover ;         // Crossover operator
	    Operator  mutation  ;         // Mutation operator
	    Operator  selection ;         // Selection operator
	                
	    //int bits ; // Length of bit string in the OneMax problem
	    HashMap  parameters ; // Operator parameters

	    //int bits = 512 ;
	    //problem = new OneMax(bits);
	 
	    problem = new Sphere("Real", 10) ;
	    //problem = new Easom("Real") ;
//	    problem = new Griewank("Real", 10) ;
	    
	    algorithm = new IslandLTGAController(problem) ; // Island GA
//	    algorithm = new IslandGa(problem) ; // Generational GA
	   // algorithm = new ssGA(problem); // Steady-state GA
	    //algorithm = new scGA(problem) ; // Synchronous cGA
	    //algorithm = new acGA(problem) ;   // Asynchronous cGA
	    
	    /* Algorithm parameters*/
	    algorithm.setInputParameter("populationSize",100);
	    algorithm.setInputParameter("maxEvaluations", 1000000);
	    
	    // Mutation and Crossover for Real codification 
	    parameters = new HashMap() ;
	    parameters.put("probability", 0.9) ;
	    parameters.put("distributionIndex", 20.0) ;
	    crossover = CrossoverFactory.getCrossoverOperator("SBXCrossover", parameters);                   

	    parameters = new HashMap() ;
	    parameters.put("probability", 1.0/problem.getNumberOfVariables()) ;
	    parameters.put("distributionIndex", 20.0) ;
	    mutation = MutationFactory.getMutationOperator("PolynomialMutation", parameters);                    
	    
	    /*
	    // Mutation and Crossover for Binary codification 
	    parameters = new HashMap() ;
	    parameters.put("probability", 0.9) ;
	    crossover = CrossoverFactory.getCrossoverOperator("SinglePointCrossover", parameters);                   

	    parameters = new HashMap() ;
	    parameters.put("probability", 1.0/bits) ;
	    mutation = MutationFactory.getMutationOperator("BitFlipMutation", parameters);                    
	    */
	    /* Selection Operator */
	    parameters = null ;
	    selection = SelectionFactory.getSelectionOperator("BinaryTournament", parameters) ;                            
	    
	    /* Add the operators to the algorithm*/
	    algorithm.addOperator("crossover",crossover);
	    algorithm.addOperator("mutation",mutation);
	    algorithm.addOperator("selection",selection);
	 
	    /* Execute the Algorithm */
	    double initTime = System.currentTimeMillis();
	    SolutionSet population = algorithm.execute();
	    
	    double estimatedTime = System.currentTimeMillis() - initTime;
	    System.out.println("Total execution time: " + estimatedTime);

	    /* Log messages */
	    System.out.println("Objectives values have been writen to file FUN");
	    population.printObjectivesToFile("FUN");
	    System.out.println("Variables values have been writen to file VAR");
	    population.printVariablesToFile("VAR");          
	  } //main
	} // GA_main
